package com.gayworld.controller.web.places;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gayworld.common.CommonJson;
import com.gayworld.common.Constant;
import com.gayworld.controller.helper.Constants;
import com.gayworld.controller.helper.MessageResourcesI18n;
import com.gayworld.controller.vo.ActionVO;
import com.gayworld.model.entity.Places;
import com.gayworld.model.noentity.Icon;
import com.gayworld.service.model.PlacesService;

@Controller
@RequestMapping(value = "/places")
public class PlacesController extends BaseController {

	@Autowired
	private PlacesService placesService;
	

	private final Logger logger = LogManager.getLogger(this.getClass()
			.getName());

	@RequestMapping(value = "/home.do")
	public ModelAndView home() {
		logger.debug("START");
		ModelAndView modelAndView = new ModelAndView("places_home");
		// TODO cambiarlo lo tiene q coger de BBDD.
		modelAndView.addObject("myposition_icon", 0);
		modelAndView.addObject("INIT_ZOOM", Constant.INIT_ZOOM);
		modelAndView.addObject("MAX_RESULT_PLACES", Constant.MAX_RESULT_PLACES);
		
		modelAndView.addObject("icon_url_list", placesService.getIconList());

		modelAndView.addObject("places_types", placesService.getIconList());
		
		
		return modelAndView;
	}

	@RequestMapping(value = "/list.do", method = RequestMethod.GET)
	public @ResponseBody List<Places> list(
			@RequestParam(required = true) Double neLng,
			@RequestParam(required = true) Double neLat,
			@RequestParam(required = true) Double swLng,
			@RequestParam(required = true) Double swLat,
			@RequestParam(required = true) List<Integer> iList) {
		logger.debug("START");
		logger.debug("iList "+iList.toString());
		List<Places> pList = placesService.findInsideOfSquare(neLng,
				neLat, swLng, swLat, iList, Constant.MAX_RESULT_PLACES);
		logger.debug("pList "+pList.size());

		
		Locale locale = LocaleContextHolder.getLocale();
		logger.debug("getDisplayLanguage "+locale.getDisplayLanguage());
		
		
		 
		return pList;
	}
	
	@RequestMapping(value = "/remove.do", method = RequestMethod.GET)
	public @ResponseBody CommonJson remove(
			@RequestParam(required = true) String reason,
			@RequestParam(required = true) Integer placeId,
			HttpServletRequest request) {
		logger.debug("START");		 
		String userIpAddress = request.getRemoteAddr();
		logger.debug("user ip :: " + userIpAddress);
		logger.debug("reason :: "+reason);
		logger.debug("placeId :: "+placeId);
		
		ActionVO a = new ActionVO();
		a.setPetitionReason(reason);
		a.setPetitionType(Constants.DELETE_PLACE);
		a.setUserIp(userIpAddress);
		a.setPlaceId(placeId);
		placesService.removePlace(a);
		logger.debug("peticion de borrado agregada.");		 
		CommonJson cj = new CommonJson();
		cj.setMessage(message.getMessage("places.ui.remove_place"));
		 	 
		 
		return cj;
	}
	
	
	/**
	 * @param placesService the placesService to set
	 */
	public void setPlacesService(PlacesService placesService) {
		this.placesService = placesService;
	}
	 
}
